package view;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import trans.LoginInfoBean;
import utils.UtilsBean;

import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport {
	private List<MenuGroup> menu = new ArrayList<MenuGroup>();
	
	public List<MenuGroup> getMenu() {
		return menu;
	}

	public void setMenu(List<MenuGroup> menu) {
		this.menu = menu;
	}

	public String execute(){
		
		LoginInfoBean manager = (LoginInfoBean)UtilsBean.getLoginManager();
		if (manager == null){	
			return SUCCESS;
		}
		JSONArray a = JSONArray.fromObject(manager.getMenuItems());
		for (int m=0; m<a.size(); m++){
			JSONObject b = a.getJSONObject(m);
			MenuItem item = MenuItemList.find(b.getString("id"));
			if (item == null){
				continue;
			}
			MenuGroup menuGroup = new MenuGroup(b.getString("id"), item.getTitle());
			JSONArray c=b.getJSONArray("items");
			for (int n=0;n<c.size();n++){
				item = MenuItemList.find(c.getString(n));
				if (item != null){
					menuGroup.AddItem(item);
				}
			}
			menu.add(menuGroup);
		}

		return SUCCESS;
	}
}
